<template>
  <el-dialog title="分配角色" :visible.sync="visible" width="80%" top="5vh" append-to-body>
    <SearchWithOperate
      :form-options="formOptions"
      btn-items="add"
      @onAdd="openSelectUser"
      @onSearch="handleQuery"
    >
      <template v-slot:otherBtn>
        <el-tooltip
          class="operate"
          :open-delay="600"
          content="批量取消授权"
          placement="bottom"
        >
          <el-button
            v-hasPermi="['system:role:remove']"
            class="operate"
            type="primary"
            plain
            size="small"
            icon="iconfont icon-opt-auth-cancel"
            @click="cancelAuthUserAll"
          />
        </el-tooltip>
      </template>
    </SearchWithOperate>

    <el-table
      v-loading="loading"
      tooltip-effect="light"
      :data="roleList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" />
      <el-table-column label="角色编号" prop="roleId" show-overflow-tooltip />
      <el-table-column label="角色名称" prop="roleName" show-overflow-tooltip />
      <el-table-column label="权限字符" prop="roleKey" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:role:remove']"
            size="mini"
            type="primary"
            plain
            @click="cancelAuthUser(scope.row)"
          >取消授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <select-role ref="select" :menu-id="queryParams.menuId" :app-id="queryParams.appId" @ok="handleQuery" />
  </el-dialog>
</template>

<script>
import { getMenuRoles } from '@/api/system/role'
import { authRoleCancel, authRoleCancelAll } from '@/api/system/menu'
import selectRole from './SelectRole'

export default {
  name: 'AuthUser',
  dicts: ['sys_normal_disable'],
  components: { selectRole },
  data() {
    return {
      formOptions: [{
        label: '角色名称:',
        prop: 'roleName',
        type: 'input',
        placeholder: '请输入角色名称'
      }],
      ids: [],
      // 遮罩层
      loading: true,
      // 选中用户组
      roleIds: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户表格数据
      roleList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appId: 0,
        menuId: 0,
        roleName: undefined
      },
      visible: false
    }
  },
  methods: {
    show(menuId, appId, ids) {
      this.resetForm('queryForm')
      this.ids = ids
      this.queryParams.menuId = menuId
      this.queryParams.appId = appId
      this.visible = true
      this.getList()
    },
    /** 查询授权用户列表 */
    getList() {
      this.loading = true
      getMenuRoles(this.queryParams).then(response => {
        this.roleList = response.data.list
        this.total = response.data.total
        this.loading = false
      }
      )
    },
    // 返回按钮
    handleClose() {
      const obj = { path: '/system/menu' }
      this.$tab.closeOpenPage(obj)
    },
    /** 搜索按钮操作 */
    handleQuery(param) {
      if (param) {
        this.queryParams = Object.assign(this.queryParams, param)
      }
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.roleIds = selection.map(item => item.roleId)
      this.multiple = !selection.length
    },
    /** 打开授权用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show(this.ids)
    },
    /** 取消授权按钮操作 */
    cancelAuthUser(row) {
      const menuId = this.queryParams.menuId
      this.$modal.confirm('确认要取消授权"' + row.roleName + '"吗？').then(function() {
        return authRoleCancel({ roleId: row.roleId, menuId: menuId })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('取消授权成功')
      }).catch(() => {})
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll() {
      if (this.roleIds.length > 0) {
        this.$modal.confirm('是否取消选中角色授权数据项？').then(() => {
          return authRoleCancelAll({ menuId: this.queryParams.menuId, roleIds: this.roleIds.join(',')
          }).then(() => {
            this.getList()
            this.$modal.msgSuccess('取消授权成功')
          })
        }).catch(() => {})
      } else {
        this.$modal.msgWarning('未选中数据')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
